import {Injectable} from '@angular/core';
import {Banner} from '../module/banner';
import {Http, URLSearchParams} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import {HotSearch} from '../module/hotSearch';
import {ProjectCategory} from '../module/projectCategory';
import {Project} from '../module/project';
import {ProjectRank} from '../module/projectRank';
import {HeatProject} from '../module/HeatProject';
import {BusinessRank} from '../module/businessRank';
import {Money} from '../module/money';

const BASE_RUL = 'http://bidsys.315jmw.cn';
const GET_BANNER_LIST = BASE_RUL + '/api/banner/getBannerList/';
const GET_HOT_SEARCH_LIST = BASE_RUL + '/api/hotSearch/getHotSearchList/';
const GET_PROJECT_CATEGORY = BASE_RUL + '/api/project/getProjectCategory/';
const GET_PROJECT = BASE_RUL + '/api/project/getProject/';
const GET_HEAT_PROJECT_LIST = BASE_RUL + '/api/project/getHeatProjectList/';
const GET_CATEGORY_PROJECT_LIST = BASE_RUL + '/api/project/getCategoryProjectList/';
const GET_BUSINESS_RANK_LIST = BASE_RUL + '/api/project/getBusinessRankList/';
const GET_PROJECT_RANK_LIST = BASE_RUL + '/api/project/getProjectRankList/';
const GET_RELATED_PROJECT_LIST = BASE_RUL + '/api/project/getProjectRelatedList/';
const POST_COMMENT = BASE_RUL + '/api/comment/postComment/';
const GET_MONEY_LIST_BYPOSITION = BASE_RUL + '/api/money/getMoneyList/';

@Injectable()
export class HttpService {

  constructor(private http: Http) {
  }

  /**
   *  获取Banner列表
   */
  getBannerList(): Promise<Banner[]> {
    return this.http.get(GET_BANNER_LIST)
      .toPromise()
      .then(response => response.json().data as Banner[])
      .catch(this.handleError);
  }

  /**
   * 提交留言
   */
  postComment(projectId: number, name: string, phone: string, content: string): Promise<Project[]> {
    const params = new URLSearchParams();
    params.set('projectId', '' + projectId);
    params.set('name', name);
    params.set('phone', phone);
    params.set('content', content);
    return this.http.get(POST_COMMENT, {search: params})
      .toPromise()
      .then(response => response.json().data as Project[])
      .catch(this.handleError);
  }

  /**
   * 添加留言
   */
  setComment(id: number, name: string, phone: string, content: string, recommend: string): Promise<string[]> {
    const params = new URLSearchParams();
    params.set('projectId', '' + id);
    params.set('name', '' + name);
    params.set('phone', '' + phone);
    params.set('content', '' + content);
    params.set('recommend', '' + recommend);
    return this.http.get(POST_COMMENT, {search: params})
      .toPromise()
      .then(response => response.json() as string[])
      .catch(this.handleError);
  }

  /**
   * 获取商机排行榜
   * @returns {Promise<any>}
   */
  getBusinessRankList(): Promise<BusinessRank[]> {
    return this.http.get(GET_BUSINESS_RANK_LIST)
      .toPromise()
      .then(response => response.json().data as BusinessRank[])
      .catch(this.handleError);
  }

  /**
   * 获取相关项目列表
   * @param id
   * @returns {Promise<any>}
   */
  getRelatedProjectList(projectId: number): Promise<Project[]> {
    const params = new URLSearchParams();
    params.set('projectId', '' + projectId);
    return this.http.get(GET_RELATED_PROJECT_LIST, {search: params})
      .toPromise()
      .then(response => response.json().data as Project[])
      .catch(this.handleError);
  }

  /**
   * 获取HotSearch列表
   */
  getHotSearchList(): Promise<HotSearch[]> {
    return this.http.get(GET_HOT_SEARCH_LIST)
      .toPromise()
      .then(response => response.json().data as HotSearch[])
      .catch(this.handleError);
  }

  /**
   * 获取项目分类
   * @returns {Promise<any>}
   */
  getProjectCategory(): Promise<ProjectCategory[]> {
    return this.http.get(GET_PROJECT_CATEGORY)
      .toPromise()
      .then(response => response.json().data as ProjectCategory[])
      .catch(this.handleError);
  }

  /**
   * 获取分类下的项目
   */
  getCategoryProjectList(position: number, fromposition: number): Promise<Project[]> {
    const params = new URLSearchParams();
    params.set('category', '' + position);
    params.set('weight', '' + fromposition);
    return this.http.get(GET_CATEGORY_PROJECT_LIST, {search: params})
      .toPromise()
      .then(response => response.json().data as Project[])
      .catch(this.handleError);
  }

  /**
   * 获取项目分类排行榜
   */
  getProjectRank(): Promise<ProjectRank[]> {
    return this.http.get(GET_PROJECT_RANK_LIST)
      .toPromise()
      .then(response => response.json().data as ProjectRank[])
      .catch(this.handleError);
  }

  /**
   * 获取热门项目
   */
  getHeatProjectList(position: number): Promise<HeatProject[]> {
    const params = new URLSearchParams();
    params.set('endHeat', '' + position);
    return this.http.get(GET_HEAT_PROJECT_LIST, {search: params})
      .toPromise()
      .then(response => response.json().data as HeatProject[])
      .catch(this.handleError);
  }

  /**
   * 获取首页广告
   */
  getListByposition(): Promise<Money[]> {
    return this.http.get(GET_MONEY_LIST_BYPOSITION)
      .toPromise()
      .then(response => response.json().data as Money[])
      .catch(this.handleError);
  }

  /**
   * 获取项目详情
   */
  getProject(id: number): Promise<Project> {
    const params = new URLSearchParams();
    params.set('id', '' + id);
    return this.http.get(GET_PROJECT, {search: params})
      .toPromise()
      .then(response => response.json().data as Project)
      .catch(this.handleError);
  }


  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject(error.message || error);
  }
}
